package controllers;

import java.awt.GridLayout;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;

/**
 * 
 * @author Chrystopher
 * AppCtrlr is in charge of checking states in the AppState file to see whether wizard should display or not.
 */

public class AppCtrlr extends MouseAdapter{
	
	private JTextArea instructions;
	private JLabel backwards, forwards;
	private JPanel instructionsP, navigationP;
	private JFrame f;
	
	private String initText = "Welcome to the Music Data Visualization Application! " +
							"\r\n This page will help you understand how the application works ! " +
							"\r\n Click on the forward button to continue.";
	
	private String secText = "The application is composed of three panes." +
							"\r\n The details pane is on the left hand side of the screen.";
	
	private String thirdText =  "The main view pane on the center/right hand side of the screen." +
								"\r\n The time slider is located at the bottom.";
	
	private String fourthText = "The details pane displays pertinent information " +
								"\n\r over the current view selection or focus.";
	
	private String fifthText = "The main view pane allows users to visualize data," +
								"\n\r on a unique building/window relationship histogram" +
								"\n\r as decribed in the following picture.";
	
	private String sixthText = "Click on next to continue.";
	
	private String seventhText = "Simply drag the cursors of the slider " +
								 "\n\r to adjust periods and update the" +
								 "\n\r current view's data.";
	
	private JCheckBox cBox;
	
	/*Looks for the AppState file and tries to read it for wizard value.*/
	public boolean checkForInitWizard(){
		boolean wizardNeeded = true;
		
		/*
		 * thanks to Rose India for the reminder
		 * (http://www.roseindia.net/java/beginners/java-read-file-line-by-line.shtml)
		 */
		try {
			FileReader fr = new FileReader(new File("AppState"));
			BufferedReader in = new BufferedReader(fr);
			String wholeSegment, stateSegment, statusSegment;
			//while they're something to read.
			boolean leaveWhile = false;
			while(in.ready() || !leaveWhile){
				wholeSegment = in.readLine();
				//System.out.println(wholeSegment);
				stateSegment = wholeSegment.substring(wholeSegment.indexOf(wholeSegment.charAt(0)),wholeSegment.indexOf(":"));
				//System.out.println(stateSegment);
				statusSegment = wholeSegment.substring(wholeSegment.indexOf(":")+1, wholeSegment.length());
				//System.out.println(statusSegment);
				if(stateSegment.toLowerCase().equals("init_wizard_checked")){
					wizardNeeded = !Boolean.parseBoolean(statusSegment);
					leaveWhile = true;
				}
			}
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
			System.err.println("Couldn't find neccessary file AppState !");
		} catch (IOException e) {
			e.printStackTrace();
			System.err.println("Couldn't properly read from AppState !");
		}
		
		return wizardNeeded;
	}
	
	public void popInitWizard(){
		f= new JFrame("Music Data Visualization Wizard");
		instructionsP = new JPanel();
		navigationP = new JPanel();
		
		navigationP.setLayout(new GridLayout(1, 2));
		f.getContentPane().setLayout(new GridLayout(0,1));
		
		instructions = new JTextArea();
		
		instructions.setEditable(false);
		
		instructions.setText(initText);
		
		backwards = new JLabel(new ImageIcon("backwards.jpg"));
		forwards = new JLabel(new ImageIcon("forwards.jpg"));
		backwards.setEnabled(false);
		
		backwards.addMouseListener(this);
		forwards.addMouseListener(this);
		
		cBox = new JCheckBox("Do not show this wizard on start-up.");
		cBox.addMouseListener(this);
		
		instructionsP.setSize(350, 350);
		instructionsP.add(new JScrollPane(instructions));
		
		navigationP.add(backwards);
		navigationP.add(forwards);
		
		f.add(instructionsP);
		f.add(navigationP);
		f.add(cBox);
		
		f.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
		f.setSize(500, 300);
		//f.pack();
		f.setLocationRelativeTo(null);
		f.setVisible(true);
	}
	
	@Override
	public void mousePressed(MouseEvent evt){
		if(evt.getSource() != null){
			if(evt.getSource().equals(backwards)){
				if(backwards.isEnabled()){
					if(instructions.getText().equals(secText)){
						instructions.setText(initText);
						backwards.setEnabled(false);
					}
					else if(instructions.getText().equals(thirdText)){
						instructions.setText(secText);
					}
					else if(instructions.getText().equals(fourthText)){
						instructions.setText(thirdText);
					}
					else if(instructions.getText().equals(fifthText)){
						instructions.setText(fourthText);
					}
					else if(instructions.getText().equals(sixthText)){
						instructions.setText(fifthText);						
					}
					else if(instructions.getText().equals(seventhText)){
						instructions.setText(sixthText);
						JOptionPane.showMessageDialog(f, "Graphical building.", "Graphical building example", JOptionPane.OK_OPTION, new ImageIcon("building.jpg"));
						forwards.setEnabled(true);
					}
				}
			}else if(evt.getSource().equals(forwards)){
				if(forwards.isEnabled()){
					if(instructions.getText().equals(initText)){
						instructions.setText(secText);
						backwards.setEnabled(true);
					}
					else if(instructions.getText().equals(secText)){
						instructions.setText(thirdText);
					}
					else if(instructions.getText().equals(thirdText)){
						instructions.setText(fourthText);
					}
					else if(instructions.getText().equals(fourthText)){
						instructions.setText(fifthText);
					}
					else if(instructions.getText().equals(fifthText)){
						instructions.setText(sixthText);
						JOptionPane.showMessageDialog(f, "Graphical building.", "Graphical building example", JOptionPane.OK_OPTION, new ImageIcon("building.jpg"));
					}
					else if(instructions.getText().equals(sixthText)){
						instructions.setText(seventhText);
						forwards.setEnabled(false);
					}
				}
			}
			else if(evt.getSource().equals(cBox)){
				f.setVisible(false);
				f.setEnabled(false);
				try {
					FileWriter fw = new FileWriter(new File("AppState"));
					BufferedWriter bw = new BufferedWriter(fw);
					bw.write("init_wizard_checked:true");
					bw.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public static void main(String[] args) {
		AppCtrlr ctrlr = new AppCtrlr();
		System.out.println(ctrlr.checkForInitWizard());
		ctrlr.popInitWizard();
	}
	
}
